package com.hmall.item.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.PageResult;
import com.hmall.item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemService itemService;


    @GetMapping("/list")
    public PageResult<Item> queryItemByPage(@RequestParam("page") Integer page,@RequestParam("size") Integer size) {
        Page<Item> itemPage = new Page<>(page,size);
        itemPage  = itemService.lambdaQuery().eq(Item::getStatus,1).page(itemPage);
        return new PageResult<>(itemPage.getTotal(),itemPage.getRecords());
    }


    @GetMapping("/{id}")
    public Item queryItemById(@PathVariable("id") Long id){
        return itemService.queryItemById(id );
    }

    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        Item item = itemService.getById(id);
        item.setStatus(status);
        itemService.updateById(item);
    }

    @PutMapping
    public void updateItem(@RequestBody Item item){
        itemService.updateItem(item);
    }



    @PostMapping
    public void addItem(@RequestBody Item item){

        itemService.save(item);
    }

    @DeleteMapping("/{id}")
    public void deleteItem(@PathVariable("id") Long id){

        itemService.deleteItem(id);
    }

}
